package com.pbc.gmp.action;

import com.pbc.gmp.commons.base.BaseController;
import com.pbc.gmp.commons.utils.PageInfo;
import com.pbc.gmp.model.basemodel.Brand;
import com.pbc.gmp.model.basemodel.Inspectiondetail;
import com.pbc.gmp.service.IInspectiondetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.annotation.ApplicationScope;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author kjk
 * @since 2018-06-14
 */
@Controller
@RequestMapping("/inspectiondetail")
public class InspectiondetailController extends BaseController {
    @Autowired
    private IInspectiondetailService inspectiondetailService;

    @RequestMapping(value="/indsManger")
    public String brandManger() {

        return "service/inspection/InspectionManger";
    }
    @RequestMapping(value="/indsList")
    @ResponseBody
    public Object indsList(Inspectiondetail indsWhere, Integer page, Integer rows, String sort, String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        Map<String, Object> condition = new HashMap<String, Object>();

        if (indsWhere.getEqtypeid()!=null) {
            condition.put("eqtypeid", indsWhere.getEqtypeid());
        }
        if (indsWhere.getInspectionfrequency() !=null) {
            condition.put("inspectionfrequency", indsWhere.getInspectionfrequency());
        }


        pageInfo.setCondition(condition);
        inspectiondetailService.selectinsdetailByDetailWhere(pageInfo);
        return pageInfo;

    }
    @RequestMapping(value="/addPage")
    public String addPage() {
        return "service/inspection/inspectionAdd";
    }
    @RequestMapping(value="/editPage")
    public String editPage(Model model, @RequestParam Long id) {

        Inspectiondetail inspectiondetail    = inspectiondetailService.selectById(id);
        model.addAttribute("inds",inspectiondetail );


        return "service/inspection/inspectionEdit";

    }
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object add(Inspectiondetail inds) {
        if (inds.getId() == null) {
            inds.setId(0L);
            inspectiondetailService.insert(inds);
        }else {
            inspectiondetailService.updateById(inds);
        }

        return renderSuccess("更新成功");
    }
    @RequestMapping(value = "/delete", method = {RequestMethod.POST })
    @ResponseBody
    public Object delete(String ids) {
        List<Long > idlist= Arrays.asList(ids.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());

        inspectiondetailService.deleteBatchIds(idlist);
        return renderSuccess("删除成功！");
    }
	
}
